﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Xml.Linq;
using Android.App;
using Android.Content;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ComApp.Model;
using ComApp;

namespace AndroidApp
{
    public class FileManager
    {
        private static FileManager instance;
        public static FileManager Instance
        {
            get
            {
                return instance ?? (instance = new FileManager());
            }
        }

        public void OpenUrl(Context c, string url)
        {
            Intent intent = new Intent(Intent.ActionView);
            intent.SetData(Android.Net.Uri.Parse(url));
            c.StartActivity(intent);
        }

        public void OpenFolder(Context c, string folder)
        {
            Intent intent = new Intent(Intent.ActionView);
            var uri = Android.Net.Uri.Parse(folder);
            intent.SetDataAndType(uri, "resource/folder");
            c.StartActivity(intent);
        }

        public void OpenFile_del(Context c, string filePath)
        {
            Intent intent = new Intent();
            intent.AddFlags(ActivityFlags.NewTask);
            intent.SetAction(Android.Content.Intent.ActionView);

            var fileExtName = System.IO.Path.GetExtension(filePath);
            var type = this.GetMimeType(fileExtName);

            var uri = Android.Net.Uri.FromFile(new Java.IO.File(filePath));
            intent.SetDataAndType(uri, type);
            c.StartActivity(intent);
        }

        public void OpenFile(Context c, string filePath)
        {
            Intent intent = new Intent(Android.Content.Intent.ActionView);
            var file = new Java.IO.File(filePath);
            string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
            string mimetype = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
            intent.SetDataAndType(Android.Net.Uri.FromFile(file), mimetype);
            c.StartActivity(intent);
        }

        private string GetMimeType(string fileExtName)
        {
            if (!(fileExtName.Length > 0))
                return string.Empty;

            fileExtName = fileExtName.Substring(1).ToLower();
            if ((new string[] { ".txt" }).Contains(fileExtName))
                return "text/plain";

            if ((new string[] { "m4a", "mp3", "mid", "xmf", "ogg", "wav" }).Contains(fileExtName))
                return "audio";

            if ((new string[] { "3gp", "mp4" }).Contains(fileExtName))
                return "video";

            if ((new string[] { "jpg", "gif", "png", "jpeg", "bmp" }).Contains(fileExtName))
                return "image";

            if ((new string[] { "apk" }).Contains(fileExtName))
                return "application/vnd.android.package-archive";

            /* android.permission.INSTALL_PACKAGES */
            return "*";
        }
    }
}
